﻿using System;
using CI;
using CI.Mesopotamia.Dal;
using CI.Misc;
using NUnit.Framework;
using CI.Interfaces;
using CI.Organism;
using Rhino.Mocks;

namespace Mesopotamia.UnitTests.Neural
{
    [TestFixture]
    public class SynapseTests
    {
        private ISynapse _synapse;
        private IOrganism _organism;
        IHostingEnvironment _hostingEnvironment;
        SimEnv _simEnv = SimEnv.Instance;

        [SetUp]
        public void Setup()
        {
            _hostingEnvironment = new CI.Mesopotamia.MSRBHost(new MockHostingEnvironmentPhysical(), new CI.Mesopotamia.ScenarioLocalXML());
            _organism = new CI.Organism.Organism(_hostingEnvironment);
            _organism.NeuralSystem = new CI.Organism.SimpleNeuralSystem(_organism);
        }

        /// <summary>
        /// try the create synapse with null organism we should get a exception
        /// </summary>
        [Test, ExpectedException(typeof(ArgumentException), ExpectedMessage = "childOrganism")]
        public void SynapseCreateNewNullOrganismException()
        {
            _synapse = CI.Organism.Synapse.CreateNewSynapse(0, null);
        }

        /// <summary>
        /// try the create synapse with organism with null neural system we should get a exception
        /// </summary>
        [Test, ExpectedException(typeof(ArgumentException), ExpectedMessage = "childOrganism has no Neural System")]
        public void SynapseCreateNewNullNeuralSystemException()
        {
            _organism.NeuralSystem = null;
            _synapse = CI.Organism.Synapse.CreateNewSynapse(0, _organism);
        }

        /// <summary>
        /// try the create synapse with organism with no neural cells
        /// </summary>
        [Test, ExpectedException(typeof(ArgumentException), ExpectedMessage = "childOrganism has not enough Neural Cells")]
        public void SynapseCreateNewNoNeuralCellsException()
        {
            // clear the cell collection
            _organism.Cells = new CellCollection();
            _synapse = CI.Organism.Synapse.CreateNewSynapse(0, _organism);
        }

        /// <summary>
        /// Test to make sure the synapse create new works
        /// </summary>
        [Test]
        public void SynapseCreateNotNull()
        {
            // setup varibles
            _hostingEnvironment.HostingEnvironmentPhysical = MockRepository.GenerateStub<IHostingEnvironmentPhysical>();
            _hostingEnvironment.HostingEnvironmentPhysical.Stub(x => x.Registered(null)).Return(false).IgnoreArguments();
            var dal = new TestDAL();
            // create a organism
            dal.CreateiRobot(0, 0, _hostingEnvironment, true);
            // set the organism
            _organism = _hostingEnvironment.Organisms.Alive[0];

            // try creating one
            _synapse = CI.Organism.Synapse.CreateNewSynapse(0, _organism);

            // check and make sure we got a synapse back
            Assert.IsInstanceOf<ISynapse>(_synapse, "Execpted Synapse");
        }


    }
}


